package cn.well.cloud.modular.controller;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import cn.well.cloud.base.auth.annotion.Permission;
import cn.well.cloud.base.enums.WXTypeEnums;
import cn.well.cloud.base.pojo.page.LayuiPageFactory;
import cn.well.cloud.modular.model.UserInfo;
import cn.well.cloud.modular.service.IUserInfoService;
import cn.well.cloud.sys.core.exception.enums.BizExceptionEnum;
import cn.well.cloud.sys.core.log.LogObjectHolder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 *  用户详情控制器
 *
 * @author jiawei
 * @Date 2020-06-04 09:32:14
 */
@Controller
@RequestMapping("/userInfo")
public class UserInfoController extends BaseController {

    private String PREFIX = "/modular/course/userInfo/";

    @Autowired
    private IUserInfoService userInfoService;

    /**
     * 跳转到 用户详情首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "userInfo.html";
    }



    /**
     * 跳转到 onco订阅号用户详情首页
     */
    @RequestMapping("/onco")
    public String indexOnco(HttpServletRequest request) {
        String wxType = WXTypeEnums.ONCO_SUB.getCode();
        request.setAttribute("wxType",wxType);
        return PREFIX + "userInfo_subscribe.html";
    }


    /**
     * 跳转到添加 用户详情
     */
    @RequestMapping("/userInfo_add")
    public String userInfoAdd() {
        return PREFIX + "userInfo_add.html";
    }

    /**
     * 跳转到编辑 用户详情
     */
    @RequestMapping("/userInfo_edit")
    public String userInfoEdit(@RequestParam Long userInfoId,@RequestParam String wxType, HttpServletRequest request) {
		if (ToolUtil.isEmpty(userInfoId)) {
            throw new ServiceException(BizExceptionEnum.REQUEST_NULL);
        }
        request.setAttribute("wxType",wxType);
        UserInfo userInfo = userInfoService.getById(userInfoId);
        LogObjectHolder.me().set(userInfo);
        return PREFIX + "userInfo_edit.html";
    }

    /**
     * 获取 用户详情列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(value = "userInfoName", required = false) String userInfoName
                           , @RequestParam(value = "wxType", required = false) String wxType) {
    
    	Page page = LayuiPageFactory.defaultPage();
    	QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>();
    	if(StrUtil.isNotEmpty(userInfoName)){
            queryWrapper.and(wrapper -> wrapper.like("userInfo_name", userInfoName));
        }
        if(StrUtil.isNotEmpty(wxType)){
            queryWrapper.and(wrapper -> wrapper.eq("wx_type", wxType));
        }
    	IPage<Map<String, Object>> pageInfo = userInfoService.pageMaps(page, queryWrapper);
        return LayuiPageFactory.createPageInfo(pageInfo);
    }

    /**
     * 添加 用户详情
     */
    @Permission
	@RequestMapping(value = "/add")
    @ResponseBody
    public Object add(UserInfo userInfo) {
        userInfoService.save(userInfo);
        return BaseController.SUCCESS_TIP;
    }

    /**
     * 删除 用户详情
     */
    @Permission
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long userInfoId) {
        userInfoService.removeById(userInfoId);
        return BaseController.SUCCESS_TIP;
    }

    /**
     * 修改 用户详情
     */
    @Permission
    @RequestMapping(value = "/edit")
    @ResponseBody
    public Object edit(UserInfo userInfo) {
        userInfoService.updateById(userInfo);
        return BaseController.SUCCESS_TIP;
    }

    /**
     *  用户详情详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail(@RequestParam("userInfoId") Long userInfoId) {
    	UserInfo userInfo = userInfoService.getById(userInfoId);
    	return ResponseData.success(userInfo);
    }

    /**
     *  同步微信用户
     */
    @RequestMapping(value = "/syncuser")
    @ResponseBody
    public Object syncuser(@RequestParam("wxType") String wxType) {
//        userInfoService.saveSyncUserByWxType(wxType);
        return ResponseData.success();
    }

}
